﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Snowdreamist.ComponentModel
{
    public abstract class CriticalDisposableObject : IDisposable
    {
        ~CriticalDisposableObject()
        {
            this.Dispose();
        }

        public bool IsDisposed
        {
            get
            {
                return Interlocked.CompareExchange(ref this._isDisposed, 0, 0) != 0;
            }
        }

        public void Dispose()
        {
            if (Interlocked.CompareExchange(ref this._isDisposed, 1, 0) == 0)
                this.DisposeOnce();
        }

        protected abstract void DisposeOnce();

        #region private

        private int _isDisposed = 0;

        #endregion private
    }
}